package com.feinno.urcs.group.test;


import com.feinno.msrp.MsrpSession;
import com.feinno.urcs.sipservlet.LargeMessageServlet;
import com.feinno.urcs.sipservlet.SipServletRequest;
import com.feinno.urcs.sipservlet.msrp.MsrpSessionFactory;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

/**
 * Created by lvmingwei on 5/13/15.
 */
public class FileTransferServlet extends LargeMessageServlet {

    private static final String FILE_STREAM = "FILE_KEY";

    private MsrpSessionFactory msrpFactory;

    public void doInvite(SipServletRequest request){
        System.out.println(request);
        super.doInvite(request);
    }

    @Override
    protected OutputStream getOutputStream(SipServletRequest inviteRequest) {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        inviteRequest.getSession().put(FILE_STREAM, outputStream);
        return outputStream;
    }

    @Override
    protected MsrpSession createMsrpSession(SipServletRequest sipServletRequest) {
        return msrpFactory.createSession();
    }

    @Override
    protected void event(Event event) {
        if (event.getState() == State.SUCCESS) {
            ByteArrayOutputStream outputStream = (ByteArrayOutputStream) event.getInviteReuqest().getSession().get(FILE_STREAM);
            System.out.println(new String(outputStream.toByteArray()));
        }
    }
}
